<template>
    <div>
        <el-dialog 
            :title="title" 
            :visible.sync="addUserVisible"  
            width="60%"
            :before-close="handleCancel" 
            :close-on-click-modal="isClose">
            <el-form label-position="right" label-width="100px" :inline="true" :model="userInfo"
                     :rules="rules" ref="userInfo"
                     :data='currentrow'
                     style="padding: 0px 70px 0px 10px;" class="demo-ruleForm">
                
                <el-form-item label="员工姓名" prop="username">
                    <el-input v-model="userInfo.username" clearable placeholder="请输入员工姓名"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="userInfo.password" placeholder="请输入登录密码"
                              autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPassword">
                    <el-input type="password" v-model="userInfo.checkPassword"
                              placeholder="请确认登录密码"
                              autocomplete="off" clearable>
                    </el-input>
                </el-form-item>
                <el-form-item label="身份证号" prop="idnumber">
                    <el-input v-model="userInfo.idnumber" clearable placeholder="请输入身份证号"></el-input>
                </el-form-item>

                <el-form-item label="家庭地址" prop="address">
                    <el-input v-model="userInfo.address" clearable placeholder="请输入家庭地址"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" prop="phonenumber">
                    <el-input v-model="userInfo.phonenumber" clearable placeholder="请输入联系电话"></el-input>
                </el-form-item>
                 <el-form-item label="身份">
                    <el-radio-group v-model="userInfo.identity">
                        <el-radio label="用户">用户</el-radio>
                        <el-radio label="商家">商家</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="userInfo.sex">
                        <el-radio label="1">男</el-radio>
                        <el-radio label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
               
                <!-- <el-form-item label="员工类型" prop="type">
                    <el-radio-group v-model="userInfo.roleId">
                        <el-radio label="1">超级管理员</el-radio>
                        <el-radio label="2">普通员工</el-radio>
                    </el-radio-group>
                </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="onSubmit('user')">创建用户</el-button>
                <el-button @click="resetForm('user')">重置信息</el-button>
                <el-button @click="cancel('user')">取消</el-button>
            </div>
            
        </el-dialog>
    </div>
</template>

<script>

import {addUserInfo,updateUser,searchUser} from '@/network/user'

import { Message } from 'element-ui'
    export default {
        name: "AddUser",
        props: {
            addUserVisible: Boolean,
            data: [Object, Boolean],
            title: String,
            modalType:Number,
            getList:Function,
            currentrow:Object,
            identityType:Number
        },
        data() {
            let checkPhone = (rule,value,callback)=>{
                let phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
                
                if(!value){
                    callback()
                } else {
                    if(phoneReg.test(value)){
                    callback()
                    } else{
                    callback(new Error('手机号不能为空，并且是11位数字'))
                    }
                }
            }
            let checkIdnumber = (rule,value,callback)=>{
                let idnumberReg = /^[1-9]\d{5}(19|20|21)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/              
                if(!value){
                    callback()
                } else {
                    if(idnumberReg.test(value)){
                    callback()
                    } else{
                    callback(new Error('身份证号码不能为空'))
                    }
                }
            }
            let checkPwd = (rule,value,callback)=>{
                let pwdReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/
                
                if(!value){
                    callback()
                } else {
                    if(pwdReg.test(value)){
                    callback()
                    } else{
                        callback(new Error('密码是6-15位的数字和字母组成'))
                    }
                }
            }
            let checkAgainpwd = (rule,value,callback)=>{
                // let pwdReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/
                
                if(!value){
                    callback()
                } else {
                    if(value === this.userInfo.password ){
                        callback()
                    } else{
                        callback(new Error('两次输入密码不一致'))
                    }
                }
            }
            let checkUserName = (rule,value,callback)=>{
                let usernameReg = /^[\u4e00-\u9fa5]{2,5}$/
                
                if(!value){
                    callback()
                } else {
                    if(usernameReg.test(value)){
                    callback()
                    } else{
                    callback(new Error('用户名不能为空，且是 2~5个汉字组成'))
                    }
                }
            }
            return {
                showDialog:false,
                userInfo: {
                   
                    username: '',
                    identity:'',
                    sex: '1',
                    phonenumber:'',
                    address: '',
                    idnumber:'',
                   
                    userStatus: '1',
                    password: '',
                    checkPassword: ''
                },
                isClose:false,
                rules:{
                    username:[
                        {required : true , message : '请输入2~5位昵称',trigger: 'blur',validator:checkUserName},
                       
                    ],
                    address:[
                        {
                            required : true , message : '请输入地址',trigger: 'blur'},                       
                    ],
                    idnumber:[
                        {
                            required : true , 
                            message : '请输入合法的身份证号码',
                            trigger: 'blur',
                            validator:checkIdnumber
                        },                       
                    ],
                    password:[
                        {required : true , message : '密码是6-15位的数字和字母组成',trigger: 'blur',validator:checkPwd},                       
                    ],
                    checkPassword:[
                         {
                            required:'true',
                            trigger:'blur',                            
                            validator:checkAgainpwd,
                            message:'两次输入密码不一致',
                        }
                    ],
                    phonenumber: [
                        {
                        required:'true',
                        trigger:'blur',                            
                        validator:checkPhone,
                        message:'手机号不能为空，并且是11位数字',
                        }
                    ],

                }
            }
        },
        //#region 
        // watch() {
        //     // var validatePass = (rule, value, callback) => {
        //     //     if (value === '') {
        //     //         callback(new Error('请输入密码'));
        //     //     } else {
        //     //         if (this.userInfo.checkPassword !== '') {
        //     //             this.$refs.user.validateField('checkPassword');
        //     //         }
        //     //         callback();
        //     //     }
        //     // };
        //     // var validatePass2 = (rule, value, callback) => {
        //     //     if (value === '') {
        //     //         callback(new Error('请再次输入密码'));
        //     //     } else if (value !== this.userInfo.password) {
        //     //         callback(new Error('两次输入密码不一致!'));
        //     //     } else {
        //     //         callback();
        //     //     }
        //     // };
        //     // var checktel = (rule, value, callback) => {
        //     //     if (!value || value === "") {
        //     //         return callback(new Error('手机号码不能为空'));
        //     //     }
        //     //     if (!(/^1[3|4|5|7|8|9][0-9]\d{4,8}$/.test(value))) {
        //     //         callback(new Error('请输入合法的手机号码'));
        //     //     } else {
        //     //         callback();
        //     //     }
        //     // };
        //     // var checkidentity = (rule, value, callback) => {
        //     //     if (!value || value === "") {
        //     //         return callback(new Error('身份证号不能为空'));
        //     //     }
        //     //     if (!(/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(value))) {
        //     //         callback(new Error('请输入合法的身份证号码'));
        //     //     } else {
        //     //         callback();
        //     //     }
        //     // };
        //     // return {
        //         // userInfo: {
        //         //     // userId: '',
        //         //     username: '',
        //         //     // realName: '',
        //         //     // identity: '',
        //         //     sex: '1',
        //         //     phonenumber:'',
        //         //     address: '',
        //         //     idnumber:'',
        //         //     // phone: '',
                   
        //         //     // roleId: '1',
        //         //     // roleName: '',
        //         //     userStatus: '1',
        //         //     password: '',
        //         //     checkPassword: ''
        //         // },
        //         // rules: {
        //         //     userName: [
        //         //         {required: true, message: '请输入用户名', trigger: 'blur'},
        //         //         {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        //         //     ],
        //         //     password: [
        //         //         {required: true, validator: validatePass, trigger: 'blur'}
        //         //     ],
        //         //     checkPassword: [
        //         //         {required: true, validator: validatePass2, trigger: 'blur'}
        //         //     ],
        //         //     realName: [
        //         //         {required: true, message: '请输入真实名字', trigger: 'blur'},
        //         //         {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
        //         //     ],
        //         //     identity: [
        //         //         {required: true, validator: checkidentity, trigger: 'blur'}
        //         //     ],
        //         //     address: [
        //         //         {required: true, message: '请输入家庭地址', trigger: 'blur'}
        //         //     ],
        //         //     phone: [
        //         //         {required: true, validator: checktel, trigger: 'blur'},
        //         //     ]
        //         // }
        //     // }
        // },
        //#endregion
        created() {
          // this.addUserInfo()
        //   console.log('reee');
        this.showDialog = this.addUserVisible

        this.showCurrent()
        console.log(this.currentrow);
        // this.sendUserinfo()
        console.log(this.identityType);
        },
        methods: {
            showCurrent(){
                console.log(this.currentrow);
                // if(this.currentrow[id] === this.userInfo[id]){
                //     console.log('ddddd');
                // }
            },
            // openAddUser() {
            //     this.$nextTick(function () {
            //         this.$refs['user'].resetFields();
            //         for (var key in this.entity) {
            //             if (this.entity.hasOwnProperty(key)) {
            //                 this.entity[key] = '';
            //             }
            //         }
            //     });
            // },

            //关闭弹窗的时候
            handleCancel() {
                this.$refs.userInfo.resetFields()
            },

           

            onSubmit(){
                const _Message = Message     
                console.log(this.showDialog);
                this.$refs.userInfo.validate((valid)=>{
                    console.log(valid);
                    // 若 验证成功
                    if(valid){
                        console.log(this.userInfo);
                        // 新增
                        if(this.modalType === 0) {
                            console.log('此时是新增');
                            // 查找所有，看看有没有
                            let phonenumber = this.userInfo.phonenumber 
                            let identity = this.userInfo.identity
                            searchUser({phonenumber,identity}).then(res=>{
                                console.log(res);
                                if(res.data.code == 1){
                                    _Message({
                                        type:'warning',
                                        message:res.data.data.message
                                    })    
                                    this.$refs.userInfo.resetFields()                                   
                                } else {
                                    addUserInfo(this.userInfo).then((res)=>{
                                        console.log(res);
                                        this.getList()
                                        // this.$emit('closeDialog')
                                         _Message({
                                            type:'success',
                                            message:'注册成功'
                                        })  
                                      this.$refs.userInfo.resetFields()                               
                                    })
                                }
                            })
                            
                            // 编辑
                        } else {
                            console.log('此时是编辑');
                                // this.currentrow = JSON.parse(JSON.stringify(this.currentrow))
                                updateUser(this.userInfo).then(()=>{
                                    console.log(this.currentrow);
                                    this.getList()
                                    // this.$refs.userInfo.resetFields()                                
                                })
                        }
                       
                        this.$emit('val-change')
                    } else {
                        _Message({
                            type:'error',
                            message:'请输入正确的信息格式'
                        })
                        return;
                    }
                })
            },


            resetForm(formName) {
                this.$refs.userInfo.resetFields();
            },
            cancel(){
                this.handleCancel()
                this.$emit('cancel')
            },
            // checkUserName(){
            //     let app = this;
            //     let data={
            //         userName:app.userInfo.userName
            //     }
            //     app.$Api.checkUserName(data, function (result) {
            //         if (result.result == "false") {
            //             app.$notify({
            //                 title: '温馨提示：',
            //                 message: result.message,
            //                 type: 'warning'
            //             });
            //             app.userInfo.userName='';
            //         }
            //     });
            // },
            sendUserinfo(){
                console.log('打算说');
            }
        },
        mounted() {
            // console.log(this.currentrow);
            this.sendUserinfo()
        },
    }
</script>

<style scoped>

</style>

